#pragma once
#include <GL/gl.h>
#include <cstdio>
#include "../gl/Drawabe.h"

#include "model.h"

class View {
private:
    Labirint * model;
    int h_size;
    int v_size;
    int cell_size;
public:
    View(Labirint * model) {
        this->model = model;
        h_size = model->cells->sz_x * (cell_size + 1) + 1;
        v_size = model->cells->sz_y * (cell_size + 1) + 1;
        cell_size = 4;
    }

    int xPos(int cell_id, bool widh_grid = true) {
        return cell_id * (cell_size + 1);
    }

    int yPos(int cell_id, bool widh_grid = true) {
        return cell_id * (cell_size + 1);
    }

    void draw_lab() {
        glColor4f(0, 1, 0, 0.5);
        glBegin(GL_QUADS);
        for (int x=0;x<model->cells->sz_x;x++) {
            int xp = xPos(x, false);
            for (int y = 0; y < model->cells->sz_y; y++) {
                Cell *cell = this->model->cells->get(x, y);
                if (cell->borders.up) {
                    int yp = yPos(y, false);
                    glVertex2i(xp, yp);
                    glVertex2i(xp + cell_size, yp);
                    glVertex2i(xp + cell_size, yp + 1);
                    glVertex2i(xp, yp + 1);
                }
                if (cell->borders.left) {
                    int yp = yPos(y, false);
                    glVertex2i(xp, yp);
                    glVertex2i(xp, yp + cell_size);
                    glVertex2i(xp + 1, yp + cell_size);
                    glVertex2i(xp + 1, yp);
                }
            }
        }

        for (int x=0;x<model->cells->sz_x;x++) {
            int y = model->cells->sz_y - 1;
            int yp = yPos(y+1, false);
            {
                Cell *cell = this->model->cells->get(x, y);
                if (cell->borders.down) {
                    int xp = xPos(x, false);
                    glVertex2i(xp, yp);
                    glVertex2i(xp + cell_size, yp);
                    glVertex2i(xp + cell_size, yp + 1);
                    glVertex2i(xp, yp + 1);
                }
            }
        }

        for (int y=0;y<model->cells->sz_y;y++) {
            int x = model->cells->sz_x - 1;
            int xp = xPos(x+1, false);
            {
                Cell *cell = this->model->cells->get(x, y);
                if (cell->borders.right) {
                    int yp = yPos(y, false);
                    glVertex2i(xp, yp);
                    glVertex2i(xp, yp + cell_size);
                    glVertex2i(xp + 1, yp + cell_size);
                    glVertex2i(xp + 1, yp);
                }
            }
        }
        glEnd();

        for (int i=0;i<this->model->fillers_count;i++) {
            int xp = xPos(this->model->fillers[i]->x, false) + 1;
            int yp = yPos(this->model->fillers[i]->y, false) + 1;
            glColor4f(1, 0, 0, 0.5);
            glBegin(GL_QUADS);
            glVertex2i(xp, yp);
            glVertex2i(xp, yp + cell_size);
            glVertex2i(xp + cell_size, yp + cell_size);
            glVertex2i(xp + cell_size, yp);
            glEnd();
        }
    }

    void draw() {
        glClear(GL_COLOR_BUFFER_BIT);
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        glColor3d(64, 0, 0);
        glLineWidth(2);
        glTranslatef(20, 20, 0);
        //draw_grid();
        draw_lab();

        glDisable(GL_TEXTURE_2D);
        glDisable(GL_BLEND);
        glFlush(); //don't need this with GLUT_DOUBLE and glutSwapBuffers
        glBindTexture(GL_TEXTURE_2D, 0);
        glutSwapBuffers();
    }
};